package com.cn.jx.manager.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.cn.jx.manager.po.Resource;

/**
 * <p>ResourceRepository 资源mapper层操作数据库的接口
 * <p>实现<{@link BaseRepository <tt>公用</tt>}}
 * 
 * @author <h1>jiangxu2  2019年11月21日</h1>
 * @version v1.11.20.2019
 * @since 1.0
 * 
 */
public interface ResourceRepository extends JpaRepository<Resource,String>,JpaSpecificationExecutor<Resource>{
	
	/**<p>定义HQL查询 根据父id查询所有有效的子资源 根据order 排序{@link Resource}*/
	@Query("from Resource  where parentId=:pid and delFlg=0 and validFlg=1  order by sort asc  ")
	public List<Resource> findAllResourceByParent(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 根据父id查询所有有效的子资源 类型是链接模块外部链接的  根据order 排序{@link Resource}*/
	@Query("from Resource  where parentId=:pid and delFlg=0 and validFlg=1 and type in('LINK','MODULE','OUT_LINK') order by sort asc ")
	public List<Resource> findAllResourceByTypeAndParent(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 根据父id，项目所属查询所有有效的子资源 类型是链接模块外部链接的 ，范围是传入的资源编码集合内 根据order 排序{@link Resource} <p> 左侧菜单用于*/
	@Query(" from Resource "
			+ " where apply=:apply and parentId=:pid and delFlg=0 and validFlg=1 and type in('LINK','MODULE','OUT_LINK')"
			+ " and code in(:resourceCodes) order by sort asc  ")
	public List<Resource> findAllResourceByPermissions(@Param(value = "apply") String apply,@Param(value = "pid") String pid,@Param("resourceCodes")List<String> resourceCodes);
	
	/**<p>定义HQL查询 根据用户ID集合查询资源集合 {@link Resource} */
	@Query(" from Resource where delFlg=0 and validFlg=1 and id in(:ids) ")
	public List<Resource> findResourceByIds(@Param("ids")List<String> ids);
	
	/**<p>定义HQL查询 所有有效资源集合{@link Resource} */
	@Query(" from Resource  where delFlg=0 and validFlg=1 ")
	public List<Resource> findAllResource();
	
	/**<p>定义HQL查询 根据父节点code查询数量，有数量说明有子集合 不能删除 判断用 {@link Resource} */
	@Query("select count(1) from Resource  where parentId=:pid and delFlg=0 and validFlg=1")
	public Integer findCountResourceByParent(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 所有有效资源集合{@link Resource} */
	@Query(" select code from Resource ")
	public List<String> findAllResourceCode();
	
	/**<p>定义HQL查询 所有有效资源集合{@link Resource} */
	@Query(" from Resource  where code=:code ")
	public Resource findResourceByCode(@Param(value = "code") String code);
	
	
	@Query("select rc from Resource rf inner join Resource rc on rf.id=rc.parentId where rf.code=:code and rc.delFlg=0 and rc.validFlg=1 ")
	public List<Resource> findAllResourceByParentCode(@Param(value = "code") String code);
}
